using KinonekoSoftware.CodariaDB.Documents.Templates;
using EVFF_InstanceType = KinonekoSoftware.Extensions.Reflections.EnumValuesFilteringFactory<KinonekoSoftware.CodariaDB.InstanceType>;

namespace KinonekoSoftware.Studio.Pages.Theories
{
    public sealed class NewInstanceTemplateViewModel : ObjectDialog<InstanceTemplate>
    {
        public static readonly EVFF_InstanceType         _filteredInstanceType = new EVFF_InstanceType(a => a != InstanceType.Custom || a != InstanceType.Instance);
        public static readonly IEnumerable<InstanceType> CommonInstanceTypes   = _filteredInstanceType.Values;
        private                bool                      _useCustomType;
        private                string                    _customType;
        private                InstanceType              _builtinType;

        public static async Task<Result<InstanceTemplate>> New() => await Dialog<InstanceTemplate, NewInstanceTemplateViewModel>();

        protected override void CreateValidationRules(ICollection<DialogRule> rules)
        {
            rules.Add(DialogRule.Create<NewInstanceTemplateViewModel>(TextSR.Parameter_Empty_Value, x =>
            {
                if (x.UseCustomType && !string.IsNullOrEmpty(x.CustomType))
                {
                    return true;
                }

                if (!x.UseCustomType)
                {
                    return true;
                }

                return false;
            }));
        }

        protected override InstanceTemplate OnFinish(bool editing)
        {
            if (UseCustomType)
            {
                return new InstanceTemplate
                {
                    Id            = ID.Get(),
                    UseCustomType = UseCustomType,
                    CustomType    = CustomType,
                    BuiltinType   = BuiltinType,
                    Definitions   = new ObservableCollection<DataPartDefinition>(),
                    Properties    = new ObservableCollection<CompositeItem>
                    {
                        InstanceTemplateGenerator.Property("名字", KnownMetadatas.Name, false),
                    },
                };
            }

            return InstanceTemplateGenerator.Generate(BuiltinType);
        }

        public IEnumerable<InstanceType> InstanceTypes => CommonInstanceTypes; 
        
        /// <summary>
        /// 获取或设置 <see cref="BuiltinType"/> 属性。
        /// </summary>
        public InstanceType BuiltinType
        {
            get => _builtinType;
            set
            {
                SetValue(ref _builtinType, value);
                RaiseUpdated(nameof(Name));
            }
        }

        private bool _useTreeGallery;

        /// <summary>
        /// 获取或设置 <see cref="UseTreeGallery"/> 属性。
        /// </summary>
        public bool UseTreeGallery
        {
            get => _useTreeGallery;
            set
            {
                //
                // notice: 树形Gallery尚未开放，因为我们觉得树形结构需要程序决定功能
                // BuiltinType = value ? InstanceType.Custom : InstanceType.Custom;
                SetValue(ref _useTreeGallery, value);
            }
        }

        /// <summary>
        /// 获取或设置 <see cref="CustomType"/> 属性。
        /// </summary>
        public string CustomType
        {
            get => _customType;
            set
            {
                SetValue(ref _customType, value);
                RaiseUpdated(nameof(Name));
            }
        }

        /// <summary>
        /// 获取或设置 <see cref="UseCustomType"/> 属性。
        /// </summary>
        public bool UseCustomType
        {
            get => _useCustomType;
            set
            {
                SetValue(ref _useCustomType, value);
                RaiseUpdated(nameof(Name));
            }
        }

        [Runtime]
        public string Name
        {
            get
            {
                if (UseCustomType)
                {
                    return CustomType;
                }

                return LanguageManager.GetEnum(BuiltinType);
            }
        }
    }
}